//
// Created by ts on 2023/5/11.
//

#ifndef LEARN_VULKAN_GEOMETRYDEMO_H
#define LEARN_VULKAN_GEOMETRYDEMO_H

#include "Geometry.h"

static bool getCube(CGeometry *out_geometry, float size) {

    //    v6----- v5
    //   /|      /|
    //  v1------v0|
    //  | |     | |
    //  | |v7---|-|v4
    //  |/      |/
    //  v2------v3

    float left      =   size * -0.5f;
    float right     =   size * 0.5f;
    float bottom    =   size * -0.5f;
    float top       =   size * 0.5f;
    float nearPlan  =   size * 0.5f;
    float farPlan   =   size * -0.5f;

    out_geometry->vertices.resize(24);
    out_geometry->indices.resize(36);

    // v0-v1-v2-v3 front
    out_geometry->vertices[0].pos =     glm::vec3(right, top, nearPlan);
    out_geometry->vertices[1].pos =     glm::vec3(left, top, nearPlan);
    out_geometry->vertices[2].pos =     glm::vec3(left, bottom, nearPlan);
    out_geometry->vertices[3].pos =     glm::vec3(right, bottom, nearPlan);
    // v0-v3-v4-v5 right
    out_geometry->vertices[4].pos =     glm::vec3(right, top, nearPlan);
    out_geometry->vertices[5].pos =     glm::vec3(right, bottom, nearPlan);
    out_geometry->vertices[6].pos =     glm::vec3(right, bottom, farPlan);
    out_geometry->vertices[7].pos =     glm::vec3(right, top, farPlan);
    // v0-v5-v6-v1 up
    out_geometry->vertices[8].pos =     glm::vec3(right, top, nearPlan);
    out_geometry->vertices[9].pos =     glm::vec3(right, top, farPlan);
    out_geometry->vertices[10].pos =    glm::vec3(left, top, farPlan);
    out_geometry->vertices[11].pos =    glm::vec3(left, top, nearPlan);
    // v1-v6-v7-v2 left
    out_geometry->vertices[12].pos =    glm::vec3(left, top, nearPlan);
    out_geometry->vertices[13].pos =    glm::vec3(left, top, farPlan);
    out_geometry->vertices[14].pos =    glm::vec3(left, bottom, farPlan);
    out_geometry->vertices[15].pos =    glm::vec3(left, bottom, nearPlan);
    // v7-v4-v3-v2 bottom
    out_geometry->vertices[16].pos =    glm::vec3(left, bottom, farPlan);
    out_geometry->vertices[17].pos =    glm::vec3(right, bottom, farPlan);
    out_geometry->vertices[18].pos =    glm::vec3(right, bottom, nearPlan);
    out_geometry->vertices[19].pos =    glm::vec3(left, bottom, nearPlan);
    // v4-v7-v6-v5 back
    out_geometry->vertices[20].pos =    glm::vec3(right, bottom, farPlan);
    out_geometry->vertices[21].pos =    glm::vec3(left, bottom, farPlan);
    out_geometry->vertices[22].pos =    glm::vec3(left, top, farPlan);
    out_geometry->vertices[23].pos =    glm::vec3(right, top, farPlan);

    // v0-v1-v2-v3 front
    out_geometry->vertices[0].texcoord =     glm::vec2(2.0f, 0.0f);
    out_geometry->vertices[1].texcoord =     glm::vec2(0.0f, 0.0f);
    out_geometry->vertices[2].texcoord =     glm::vec2(0.0f, 2.0f);
    out_geometry->vertices[3].texcoord =     glm::vec2(2.0f, 2.0f);
    // v0-v3-v4-v5 right
    out_geometry->vertices[4].texcoord =     glm::vec2(1.0f, 0.0f);
    out_geometry->vertices[5].texcoord =     glm::vec2(0.0f, 0.0f);
    out_geometry->vertices[6].texcoord =     glm::vec2(0.0f, 1.0f);
    out_geometry->vertices[7].texcoord =     glm::vec2(1.0f, 1.0f);
    // v0-v5-v6-v1 up
    out_geometry->vertices[8].texcoord =     glm::vec2(1.0f, 0.0f);
    out_geometry->vertices[9].texcoord =     glm::vec2(0.0f, 0.0f);
    out_geometry->vertices[10].texcoord =    glm::vec2(0.0f, 1.0f);
    out_geometry->vertices[11].texcoord =    glm::vec2(1.0f, 1.0f);
    // v1-v6-v7-v2 left
    out_geometry->vertices[12].texcoord =    glm::vec2(1.0f, 0.0f);
    out_geometry->vertices[13].texcoord =    glm::vec2(0.0f, 0.0f);
    out_geometry->vertices[14].texcoord =    glm::vec2(0.0f, 1.0f);
    out_geometry->vertices[15].texcoord =    glm::vec2(1.0f, 1.0f);
    // v7-v4-v3-v2 bottom
    out_geometry->vertices[16].texcoord =    glm::vec2(1.0f, 0.0f);
    out_geometry->vertices[17].texcoord =    glm::vec2(0.0f, 0.0f);
    out_geometry->vertices[18].texcoord =    glm::vec2(0.0f, 1.0f);
    out_geometry->vertices[19].texcoord =    glm::vec2(1.0f, 1.0f);
    // v4-v7-v6-v5 back
    out_geometry->vertices[20].texcoord =    glm::vec2(1.0f, 0.0f);
    out_geometry->vertices[21].texcoord =    glm::vec2(0.0f, 0.0f);
    out_geometry->vertices[22].texcoord =    glm::vec2(0.0f, 1.0f);
    out_geometry->vertices[23].texcoord =    glm::vec2(1.0f, 1.0f);

    // v0-v1-v2-v3 front
    out_geometry->vertices[0].normal =    glm::vec3(0, 0, 1);
    out_geometry->vertices[1].normal =    glm::vec3(0, 0, 1);
    out_geometry->vertices[2].normal =    glm::vec3(0, 0, 1);
    out_geometry->vertices[3].normal =    glm::vec3(0, 0, 1);
    // v0-v3-v4-v5 right
    out_geometry->vertices[4].normal =    glm::vec3(1, 0, 0);
    out_geometry->vertices[5].normal =    glm::vec3(1, 0, 0);
    out_geometry->vertices[6].normal =    glm::vec3(1, 0, 0);
    out_geometry->vertices[7].normal =    glm::vec3(1, 0, 0);
    // v0-v5-v6-v1 up
    out_geometry->vertices[8].normal =    glm::vec3(0, 1, 0);
    out_geometry->vertices[9].normal =    glm::vec3(0, 1, 0);
    out_geometry->vertices[10].normal =   glm::vec3(0, 1, 0);
    out_geometry->vertices[11].normal =   glm::vec3(0, 1, 0);
    // v1-v6-v7-v2 left
    out_geometry->vertices[12].normal =   glm::vec3(-1, 0, 0);
    out_geometry->vertices[13].normal =   glm::vec3(-1, 0, 0);
    out_geometry->vertices[14].normal =   glm::vec3(-1, 0, 0);
    out_geometry->vertices[15].normal =   glm::vec3(-1, 0, 0);
    // v7-v4-v3-v2 bottom
    out_geometry->vertices[16].normal =   glm::vec3(0, -1, 0);
    out_geometry->vertices[17].normal =   glm::vec3(0, -1, 0);
    out_geometry->vertices[18].normal =   glm::vec3(0, -1, 0);
    out_geometry->vertices[19].normal =   glm::vec3(0, -1, 0);
    // v4-v7-v6-v5 back
    out_geometry->vertices[20].normal =   glm::vec3(0, 0, -1);
    out_geometry->vertices[21].normal =   glm::vec3(0, 0, -1);
    out_geometry->vertices[22].normal =   glm::vec3(0, 0, -1);
    out_geometry->vertices[23].normal =   glm::vec3(0, 0, -1);

    out_geometry->indices = {
            0, 1, 2, 0, 2, 3,
            4, 5, 6, 4, 6, 7,
            8, 9, 10, 8, 10, 11,
            12, 13, 14, 12, 14, 15,
            16, 17, 18, 16, 18, 19,
            20, 21, 22, 20, 22, 23
    };

    return true;
}

#endif //LEARN_VULKAN_GEOMETRYDEMO_H
